package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Transactional
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //思路可参照添加购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //调用mapper层查询购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);


            if (shoppingCartDTO.getDishId() != null) {
                Integer number = shoppingCart.getNumber();
                if(number == 1){
                    //当前商品在购物车中的份数为1，直接删除当前记录
                    shoppingCartMapper.deleteById(shoppingCart.getId());
                }else {
                    //当前商品在购物车中的份数不为1，修改份数即可
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateNumberById(shoppingCart);
                }
            }else if (shoppingCartDTO.getSetmealId()!=null){
                Integer number = shoppingCart.getNumber();
                if(number == 1){
                    //当前商品在购物车中的份数为1，直接删除当前记录
                    shoppingCartMapper.deleteById(shoppingCart.getId());
                }else {
                    //当前商品在购物车中的份数不为1，修改份数即可
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateNumberById(shoppingCart);
                }
            }


        }




    /**
     * 清空购物车商品
     */
    @Transactional
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        //1.根据用户id查询购物车集合
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.拷贝属性，shoppingCartDTO 到 shoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //2.补全用户id，调用mapper查询购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);


        //3.如果已经存在，则数量+1，并更新数据库
        if (shoppingCart != null) {
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //4.如果不存在，则数量是1
            shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //4.1判断添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            //4.1.1判断若是菜品，根据菜品id查询菜品
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setDishId(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //4.1.2是套餐，根据套餐id查询套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCartMapper.insert(shoppingCart);
        }
    }
}